using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace GeorgeHerolyants.Converters
{
    internal class DirectoryConverter : IConverter
    {
        public DirectoryConverter ( )
        {
        }

        public DirectoryConverter ( string inputPath, string outputPath )
        {
            InputPath = inputPath;
            OutputPath = outputPath;
        }

        private string _inputPath;

        public string InputPath
        {
            get { return _inputPath; }
            set { _inputPath = value; }
        }

        private string _outputPath;

        public string OutputPath
        {
            get { return _outputPath; }
            set { _outputPath = value; }
        }

        private string _searchPattern = "*";

        public string SearchPattern
        {
            get { return _searchPattern; }
            set { _searchPattern = value; }
        }

        private bool _searchRecursive = false;

        public bool SearchRecursive
        {
            get { return _searchRecursive; }
            set { _searchRecursive = value; }
        }

        public void Convert ( )
        {
            DirectoryInfo input = new DirectoryInfo(InputPath);
            DirectoryInfo output = new DirectoryInfo(OutputPath);
            output.Create();

            FileInfo[] files = input.GetFiles(SearchPattern,
                SearchRecursive ? 
                SearchOption.AllDirectories : 
                SearchOption.TopDirectoryOnly );

            foreach ( FileInfo file in files )
            {
                DirectoryInfo newPath = new DirectoryInfo(
                    file.DirectoryName.Replace( input.FullName, output.FullName ) );
                newPath.Create();
                FileConverter converter = new FileConverter(
                    file.FullName, Path.Combine(newPath.FullName, file.Name));
                converter.Convert();
            }
        }
    }
}
